<?php

namespace App\Console\Commands;

use App\Models\LiftPole;
use App\Models\PayFee;
use App\Models\DateCollection;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;

class CollectOrderFix extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'collect:order-fix';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单信息汇总-fix';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * 支付记录信息定时汇总 计划任务
     * @author wangxb
     * @email  wangxiaobo@parkingwang.com
     *
     * @return mixed
     */
    public function handle()
    {

        $year  = intval( date('Y') );
        $month = intval( date('n') );
        $day   = intval( date('j') );
        $hour  = intval( date('G') ) - 1;

        // $day = 25;
        // for ($hour =0; $hour < 24; $hour++) { 
        //     $this->_mkdata($hour, $day, $month, $year);
        // }
        $this->_mkdata($hour, $day, $month, $year);
    }

    /**
     * Execute the console command.
     * 支付记录信息定时汇总 计划任务
     * @author wangxb
     * @email  wangxiaobo@parkingwang.com
     *
     * @return mixed
     */
    public function _mkdata($hour = 0, $day = 0, $month = 0, $year = 0)
    {
        $timeStart = mktime($hour, 0, 0, $month, $day, $year);
        $timeEnd   = mktime($hour+1, 0, 0, $month, $day, $year);
        Log::info('[定时任务]<支付记录汇总>：开始');
        $this->info(date('Y-m-d H:i:s', $timeStart) . ' [定时任务]<支付记录汇总>: 执行开始');
        // 定时任何 汇总时间 区间(前闭后开)

        $times = [ '[', $timeStart, $timeEnd, ')' ]; 

        $fee = new PayFee;
        $all_pay_sum = $fee->sumPayGroupByPark($times);
        $car_pay_sum = collect($fee->sumCarPayGroupByPark($times));
        $pay_type_sum = $fee->sumPayTypeGroupByPark($times);

        $freeCarCharge = LiftPole::where('out_time', '>=', $timeStart)
                                ->where('out_time', '<', $timeEnd)
                                ->selectRaw('park_code, SUM(`calc_charge`) AS pay_src_amount')
                                ->groupBy('park_code')
                                ->get()
                                ->groupBy('park_code');

        $all_pay_sum->map(function ($item) use ($timeStart, $timeEnd, $freeCarCharge) {
            if ($freeCarCharge->has($item->park_code)) {
                $freeMoney = $freeCarCharge->get($item->park_code)->first();
                $money = $freeMoney->pay_src_amount;

            } else {
                $money = 0;
            }

            DateCollection::updateOrCreate(
                [
                    'park_code' => $item->park_code,
                    'hour' => date('Y-m-d H:00:00', $timeStart)
                ],
                [
                    'pay_amount' => $item->pay_amount,
                    'pay_src_amount' => $item->pay_src_amount + $money
                ]
            );
        });

        $car_pay_sum->map(function ($value) use ($timeStart) {
            DateCollection::updateOrCreate(
                [
                    'park_code' => $value['park_code'],
                    'hour' => date('Y-m-d H:00:00', $timeStart)
                ],
                $value
            );
        });
        $pay_type_sum->map(function ($value) use ($timeStart) {
            DateCollection::updateOrCreate(
                [
                    'park_code' => $value['park_code'],
                    'hour' => date('Y-m-d H:00:00', $timeStart)
                ],
                $value->toArray()
            );
        });

        Log::info('[定时任务]<支付记录汇总>：结束');
        $this->info(date('Y-m-d H:i:s') . ' [定时任务]<支付记录汇总>: 执行结束: ');
    }
}

